"""文章模型"""
# 字段类型引入 column列
from sqlalchemy import Column, Integer, String, DateTime, func, Text, ForeignKey, text
from soulapi.db import Base, engine
# relationship SQLAlchemy ORM提供的方法
from sqlalchemy.orm import relationship


# 基于BaseNoel定义请求体的结构（json对象）
class News(Base):
    __tablename__ = 'news'  # 数据表表名，__tablename__：设置映射到的表名不设置，默认映射到类名post下
    __table_args__ = {'comment': '文章'}
    ne_id = Column(Integer, primary_key=True, index=True, autoincrement=True, server_default=text("0"),
                   comment='文章ID')  # index索引设置,autoincremen自增长
    ne_title = Column(String(100), nullable=False, server_default=text("''"), comment="文章标题")
    ne_author = Column(String(25), nullable=False, server_default=text("''"), comment="文章作者")
    ne_content = Column(Text, nullable=False, comment="文章内容")
    ne_date = Column(DateTime, nullable=False, server_default=func.now(), comment="发布时间")
    # 外键
    ad_id = Column(Integer, ForeignKey('admin.ad_id'), comment="发布文章:管理员")
    nety_id = Column(Integer, ForeignKey('newstype.nety_id'), comment="文章类别")
    # 关系(back_populates双向显示，back_populates 来指定反向访问的属性名)
    admin = relationship('Admin', back_populates='news', foreign_keys=[ad_id])
    newstype = relationship('NewsType', back_populates='news', foreign_keys=[nety_id])
    # 级联操作
    newscomm = relationship('NewsComm', back_populates='news', cascade='all')
    # 时间获取
    created_at = Column(DateTime, server_default=func.now(), comment='创建时间')
    updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now(), comment='更新时间')


# 在数据库中生成表
Base.metadata.create_all(engine)
